<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item>
        <el-input v-model="listContext.filter" :placeholder="$t('AbpTenantManagement[\'Search\']')" @keyup.enter.native="fetchData" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData">
          <i class="el-icon-search" />
        </el-button>
      </el-form-item>
      <el-button
        type="primary"
        style="float: right"
        @click="openCreateTenantDialog"
      >
        <i class="el-icon-plus" />{{ $t('AbpTenantManagement[\'NewTenant\']') }}
      </el-button>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('AbpTenantManagement[\'LoadingWithThreeDot\']')"
      :empty-text="$t('AbpTenantManagement[\'NoDataAvailableInDatatable\']')"
      border
      fit
      highlight-current-row
      height="calc(100vh - 225px)"
      @sort-change="sortChange"
    >
      <el-table-column :label="$t('AbpTenantManagement[\'Actions\']')" >
        <template slot-scope="scope">
          <el-dropdown trigger="click" @command="clickActionDropDown">
            <el-button type="primary">
              <i class="el-icon-setting" />
              {{ $t('AbpTenantManagement[\'Actions\']') }}
              <i class="el-icon-arrow-down" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{ action: 'edit', row: scope.row }">{{ $t('AbpTenantManagement[\'Edit\']') }}</el-dropdown-item>
              <el-dropdown-item :command="{ action: 'connectionStrings', row: scope.row }">{{ $t('AbpTenantManagement[\'ConnectionStrings\']') }}</el-dropdown-item>
<!--              <el-dropdown-item :command="{ action: 'features', row: scope.row }">{{ $t('AbpTenantManagement[\'Features\']') }}</el-dropdown-item>-->
              <el-dropdown-item :command="{ action: 'delete', row: scope.row }">{{ $t('AbpTenantManagement[\'Delete\']') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column :label="$t('AbpTenantManagement[\'TenantName\']')" prop="name" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('AbpTenantManagement[\'EditionName\']')" prop="name" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="margin-top: 10px">
      <el-pagination
        :page-sizes="pageContext.pageSizes"
        :page-size.sync="listContext.maxResultCount"
        :current-page.sync="pageContext.currentPage"
        :total="pageContext.totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchData"
        @current-change="handleCurrentChange"
      />
    </div>
    <create-or-edit-tenant ref="createOrEditTenant" :visible.sync="tenantDialogVisible" :context="tenantDialogCtx" @saveSucceed="fetchData" />
    <connection-strings ref="createOrEditTenant" :visible.sync="connectionStringsDialogVisible" :context="connectionStringsDialogCtx" @saveSucceed="fetchData" />
    <features ref="createOrEditTenant" :visible.sync="featuresDialogVisible" :context="featuresDialogCtx" @saveSucceed="fetchData" />
  </div>
</template>

<script>
import { getList, deleteById } from '@/api/administration/tenant/tenant'
import CreateOrEditTenant from '@/views/administration/saas/tenants/components/createOrEditTenant'
import Features from '@/views/administration/saas/tenants/components/features'
import ConnectionStrings from '@/views/administration/saas/tenants/components/connectionStrings'

export default {
  components: { CreateOrEditTenant, Features, ConnectionStrings},
  data() {
    return {
      list: null,
      listLoading: false,
      pageContext: {
        pageSizes: [10, 25, 50, 100],
        totalCount: 0,
        currentPage: 1
      },
      listContext: {
        filter: '',
        sorting: '',
        skipCount: 0,
        maxResultCount: 10
      },
      tenantDialogCtx: {
        id: '',
        title: '',
        actionMode: ''
      },
      tenantDialogVisible: false,
      featuresDialogCtx: {
        id: ''
      },
      featuresDialogVisible: false,
      connectionStringsDialogCtx: {
        id: ''
      },
      connectionStringsDialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listContext).then((response) => {
        this.list = response.items
        this.pageContext.totalCount = response.totalCount
        this.listLoading = false
      })
    },
    handleCurrentChange(currentPage) {
      this.listContext.skipCount = (currentPage - 1) * this.listContext.maxResultCount
      this.fetchData()
    },
    sortChange({ prop, order }) {
      this.listContext.sorting = ''
      if (order) {
        this.listContext.sorting = prop + ' ' + order
      }
      this.fetchData()
    },
    clickActionDropDown(ctx) {
      switch (ctx.action) {
        case 'edit':
          this.openEditTenantDialog(ctx.row.id)
          break
        case 'connectionStrings':
          this.openConnectionStringsDialog(ctx.row.id)
          break
        case 'features':
          this.openFeaturesDialog(ctx.row.id)
          break
        case 'delete':
          this.$confirm(this.$t('AbpTenantManagement[\'AreYouSure\']')).then(async _ => {
            await deleteById(ctx.row.id)
            this.$message({
              message: this.$t('AbpTenantManagement[\'Ok\']'),
              type: 'success'
            })
            this.fetchData()
          }).catch(_ => {})
          break
      }
    },
    openCreateTenantDialog() {
      this.tenantDialogVisible = true
      this.tenantDialogCtx = {
        title: this.$t('AbpTenantManagement[\'NewTenant\']'),
        actionMode: 'create'
      }
    },
    openEditTenantDialog(id) {
      this.tenantDialogVisible = true
      this.tenantDialogCtx = {
        title: this.$t('AbpTenantManagement[\'Edit\']'),
        actionMode: 'edit',
        id: id
      }
    },
    openConnectionStringsDialog(id) {
      this.connectionStringsDialogVisible = true
      this.connectionStringsDialogCtx = {
        id: id
      }
    },
    openFeaturesDialog(id) {
      this.featuresDialogVisible = true
      this.featuresDialogCtx = {
        id: id
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

